package com.sxhuayuan.parking.controller.admin;

import javax.annotation.Resource;

import com.sxhuayuan.parking.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.entity.ParkChannel;
import com.sxhuayuan.parking.service.ParkChannelService;
import com.sxhuayuan.parking.service.ParkingLotService;

/** 车场出口 */
@RestController("adminParkingLotExitController")
@RequestMapping("/admin/parkinglotexit")
public class ParkingLotExitController extends BaseController {

	@Autowired
	ParkChannelService service;
	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService lotService;

	@RequestMapping("/list")
	public RespMessage list(@RequestBody Pageable pageable) {
		Page<ParkChannel> page = service.findPage(pageable);
		return RespMessage.builder().success().setDatas(page).build();
	}

	/** 根据车场Id查询出口 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public RespMessage get(Long id) {
		ParkChannel carFieldExit = service.findOne(id);
		return RespMessage.builder().success().setDatas(carFieldExit).build();
	}

	/** 新增车场出口 */
	@RequestMapping("/save")
	public RespMessage save(@Validated @RequestBody ParkChannel exit) {
		ParkingLot lot = lotService.findOne(exit.getParkingLot().getId());
		exit.setParkingLot(lot);
		exit.setAgent(lot.getAgent());
		service.save(exit);
		return RespMessage.builder().success().build();
	}

	@RequestMapping("/update")
	public RespMessage update(@Validated @RequestBody ParkChannel carFieldExit) {
		carFieldExit.setAgent(lotService.findOne(carFieldExit.getParkingLot().getId()).getAgent());
		service.update(carFieldExit);
		return RespMessage.builder().success().build();
	}

	/** 删除 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public RespMessage delete(Long id) {

		ParkChannel one = service.findOne(id);

		/* 车场出口不存在 */
		if (one == null) {
			return RespMessage.error(RespMessage.Status.ERROR, Message.PARK_CHANNEL_NOT_EXISTS);
		}

		service.delete(one.getId());
		return RespMessage.builder().success().build();
	}
}
